﻿using System;
using DevExpress.XtraEditors;
using System.Windows.Forms;
using System.Linq;

namespace Management
{
    public partial class frmListe : XtraForm
    {
        public Control SetValueControl { get; set; }
        public ListeTipleri ListeTipi { get; set; }
        public string Filtre { get; set; }

        public enum ListeTipleri
        {
            StokKarti,
            DepoKarti,
            CariKarti,
            KullaniciListesi
        }

        public frmListe()
        {
            InitializeComponent();
        }

        private void frmListe_Load(object sender, EventArgs e)
        {
            MikroEntities ent = new MikroEntities();
            switch (ListeTipi)
            {
                case ListeTipleri.StokKarti:
                    gridControl1.DataSource = (from s in ent.Stoklar where s.sto_isim.Contains(Filtre) select new { StokAdi = s.sto_isim, StokKodu = s.sto_kod }).ToList();
                    gridView1.Columns["StokAdi"].Caption = "Stok Adı";
                    gridView1.Columns["StokKodu"].Caption = "Stok Kodu";
                    break;
                case ListeTipleri.DepoKarti:
                    gridControl1.DataSource = (from d in ent.Depolar where d.dep_adi.Contains(Filtre) select new { DepoKodu = d.dep_no, DepoAdi = d.dep_adi }).ToList();
                    gridView1.Columns["DepoAdi"].Caption = "Depo Adı";
                    gridView1.Columns["DepoKodu"].Caption = "Depo No";
                    break;
                case ListeTipleri.CariKarti:
                    gridControl1.DataSource = (from c in ent.Cariler where c.cari_unvan1.Contains(Filtre) select new { CariKodu = c.cari_kod, CariAdi = c.cari_unvan1 }).ToList();
                    gridView1.Columns["CariAdi"].Caption = "Cari Adı";
                    gridView1.Columns["CariKodu"].Caption = "Cari No";
                    break;
                case ListeTipleri.KullaniciListesi:
                    gridControl1.DataSource = (from c in ent.Kullanicilar select new { c.ID, c.Kodu, c.Adi }).ToList();
                    gridView1.Columns["ID"].Visible = false;
                    gridView1.Columns["Kodu"].Caption = "Kullanıcı Kodu";
                    gridView1.Columns["Adi"].Caption = "Kullanıcı Adı";
                    break;
                default:
                    break;
            }
            gridView1.BestFitColumns();
            gridControl1.Focus();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (gridView1.RowCount > 0 && this.SetValueControl != null)
            {
                this.SetValueControl.Tag = gridView1.GetRowCellDisplayText(gridView1.FocusedRowHandle, gridView1.Columns[1]).ToString();
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.RowCount > 0 && this.SetValueControl != null)
            {
                this.SetValueControl.Tag = gridView1.GetRowCellDisplayText(gridView1.FocusedRowHandle, gridView1.Columns[1]).ToString();
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (gridView1.RowCount > 0 && this.SetValueControl != null)
                {
                    this.SetValueControl.Tag = gridView1.GetRowCellDisplayText(gridView1.FocusedRowHandle, gridView1.Columns[1]).ToString();
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
        }
    }
}
